const findAll = (models: any) => {
  return new Promise((resolve, reject) => {
    models
      .find({}, { __v: 0 })
      .then((res: any) => {
        resolve(res);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
};
const findById: (models: any, id: any) => Promise<any> = (models, id) => {
  return new Promise<any>((resolve, reject) => {
    models
      .findById(id, { __v: 0 })
      .then((res: any) => {
        resolve(res);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
};

const findByProps = (models: any, props: any, nshow?: any): any => {
  return new Promise((resolve, reject) => {
    models
      .findOne(props, nshow)
      .then((res: any) => {
        resolve(res);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
};

const create = (models: any, item: any) => {
  return new Promise((resolve, reject) => {
    models
      .create(item)
      .then((res: any) => {
        resolve(res);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
};
const deleteById = (models: any, id: any) => {
  return new Promise((resolve, reject) => {
    models
      .findByIdAndDelete(id)
      .then((res: any) => {
        console.log(res, "delres");
        resolve(res);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
};
const modifyById = (models: any, id: any, item: any) => {
  return new Promise((resolve, reject) => {
    models
      .findByIdAndUpdate(id, item)
      .then((res: any) => {
        resolve(res);
      })
      .catch((err: any) => {
        reject(err);
      });
  });
};

export { findAll, findById, create, deleteById, modifyById, findByProps };
